package  
{
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import org.flixel.FlxG;
	import org.flixel.FlxSprite;
	
	/**
	 * ...
	 * @author Ian Guthridge
	 */
	public class Enemy extends FlxSprite 
	{
		[Embed(source = "assets/stupidguy.jpg")]
		private static var stupidGuy:Class;
		
		[Embed(source = "assets/NakedGuy_0.png")]
		private static var q0:Class;
		
		[Embed(source = "assets/NakedGuy_1.png")]
		private static var q1:Class;
		
		[Embed(source = "assets/NakedGuy_2.png")]
		private static var q2:Class;
		
		[Embed(source = "assets/NakedGuy_3.png")]
		private static var q3:Class;
		
		[Embed(source = "assets/NakedGuy_4.png")]
		private static var q4:Class;
		
		[Embed(source = "assets/Damage1.png")]
		private static var d1:Class;
		
		[Embed(source = "assets/Damage2.png")]
		private static var d2:Class;
		
		[Embed(source = "assets/Damage3.png")]
		private static var d3:Class;
		
		
		private var m_enemySprite : FlxSprite;
		
		private var m_timeSinceLastUpdate : Number = 0;
		
		private var m_quadToBlock : int = 0;
		
		private var m_damage : FlxSprite;
		

		public function Enemy(damage:FlxSprite) 
		{
			super(0,0,q0);
			health = 500;
			m_damage = damage;
			m_damage.loadGraphic( d1 );
			m_damage.visible = false;
		}
		
		override public function update():void 
		{
			if(!this.alive)
				return;
			
			m_timeSinceLastUpdate += org.flixel.FlxG.elapsed;
			m_damage.update();
			
			
			//pick new quad to block every 3 seconds
			if(m_timeSinceLastUpdate > 1)
			{
				chooseRandomQuadrant();
				m_timeSinceLastUpdate = 0;
			}
			
			if(health < 75)
			{
				m_damage.visible = true;
				m_damage.loadGraphic(d3);
			}
			else if(health < 200)
			{
				m_damage.visible = true;
				m_damage.loadGraphic(d2);
			}
			else if(health < 300)
			{
				m_damage.visible = true;	
			}
			else
			{
				m_damage.visible = false;
			}

			//remove block after 1 second. 
			if(m_timeSinceLastUpdate > 1)
			{
				m_quadToBlock = 0;
			}
			
			tryBlockQuadrant( m_quadToBlock );
		}
		
		private function tryBlockQuadrant(a_quadToBlock:int):void
		{
			//block... some animation idk..
			
			var classToLoad:Class;
			
			switch(a_quadToBlock)
			{
				case 4: classToLoad = q4;
					break;
				case 3: classToLoad = q3;
					break;
				case 2: classToLoad = q2;
					break;
				case 1: classToLoad = q1;
					break;
				default: classToLoad = q0;
					break;
			}
			
			loadGraphic( classToLoad );
		}
		
		private function chooseRandomQuadrant():void
		{
			var rand : int = Math.random() * 100;
			
			if (rand < 25)
			{
				m_quadToBlock = 1;
			}
			else if(rand < 50)
			{
				m_quadToBlock = 2;
			}
			else if(rand < 75)
			{
				m_quadToBlock = 3;
			}
			else
			{
				m_quadToBlock = 4;
			}
		}
		
		public function hit(quad:int, hitStrength:int):Boolean 
		{
			if(m_quadToBlock != quad)
			{
				health -= hitStrength;
				
				if(health <= 0)
				{
					this.kill();
				}
				
				return true;
			}
			return false;
		}
		
		override public function kill():void
		{
			this.alive = false;
			m_damage.visible = false;
			
			//do some animation maybe.. 
		}
		
	}

}